package com.devilvan.grouping;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.math.BigDecimal;

public class OrderBean implements WritableComparable<OrderBean> {
    private String orderId;
    private BigDecimal price;

    /**
     * 按订单号升序排序，相同则按照价格降序排序
     * @param o
     * @return
     */
    @Override
    public int compareTo(OrderBean o) {
        return orderId.compareTo(o.orderId) == 0 ? -price.compareTo(o.price) : orderId.compareTo(o.orderId);
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(orderId);
        out.writeUTF(price.toString());
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.orderId = in.readUTF();
        this.price = new BigDecimal(in.readUTF());
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "OrderBean{" +
                "orderId='" + orderId + '\'' +
                ", price=" + price +
                '}';
    }
}
